package com.chenyang.mediaplay.activity;

import java.util.ArrayList;

import com.chenyang.mediaplay.R;
import com.chenyang.mediaplay.R.layout;
import com.chenyang.mediaplay.R.menu;
import com.chenyang.mediaplay.adapter.MainAdapter;
import com.chenyang.mediaplay.fragment.MusicFragment;
import com.chenyang.mediaplay.fragment.VideoFragment;
import com.chenyang.mediaplay.interfaces.Keys;
import com.chenyang.mediaplay.utils.Utils;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends BaseActivity{

	private TextView tv_video;
	private TextView tv_music;
	private View view_indicator;
	private ViewPager vp_main;
	private int indicatorWidth;

	

	@Override
	public void initData() {
		changeTitleTextState(true);
		initViewPager();
	}

	/**
	 * 初始化viewpager
	 */
	private void initViewPager() {
		// TODO Auto-generated method stub
		ArrayList<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new VideoFragment());
		fragments.add(new MusicFragment());
		
		MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(),fragments);
		vp_main.setAdapter(mainAdapter);
		
		if (getIntent().getBooleanExtra(Keys.IS_FROM_AUDIO_PLAYER, false)) {
			// 如果返回true，说明是从音乐列表回来的
			vp_main.setCurrentItem(1);
			ViewHelper.setTranslationX(view_indicator, indicatorWidth);
			changeTitleTextState(false);
		}
		
		
	}

	/**
	 * 改变标题的状态
	 * @param isSelectVideo(是否选中了视频)
	 */
	private void changeTitleTextState(boolean isSelectVideo) {
		// TODO Auto-generated method stub
		//改变文本颜色
		tv_video.setSelected(isSelectVideo);
		tv_music.setSelected(!isSelectVideo);
		
		//改变文本大小
		scaleTitle(isSelectVideo ? 1.2f : 1.0f, tv_video);
		scaleTitle(!isSelectVideo ? 1.2f : 1.0f, tv_music);
		
	}

	/**
	 * 改变文本大小
	 * @param scale
	 * @param textview
	 */
	private void scaleTitle(float scale, TextView textview) {
		// TODO Auto-generated method stub
		//有过度动画
		ViewPropertyAnimator.animate(textview).scaleX(scale).scaleY(scale);
	}

	/**
	 * 初始化指示器
	 */
	private void initIndicator() {
		// TODO Auto-generated method stub
		int screenWidth = Utils.getScreenWidth(this);
		indicatorWidth = screenWidth/2;
		view_indicator.getLayoutParams().width=indicatorWidth;
		// 通知这个View去更新它的布局参数
		view_indicator.requestLayout();
		
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		tv_video = findView(R.id.tv_video);
		tv_music = findView(R.id.tv_music);
		view_indicator = findView(R.id.view_indicator);
		vp_main = findView(R.id.vp_main);
		initIndicator();
	}

	@Override
	public int getLayoutResID() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}

	@Override
	public void onClick(View v, int id) {
		// TODO Auto-generated method stub
		switch (id) {
		
		case R.id.tv_video:
			vp_main.setCurrentItem(0);
			break;
			
		case R.id.tv_music:
			vp_main.setCurrentItem(1);
			break;
			
		default:
			break;
		}
	}

	@Override
	public void initListener() {
		// TODO Auto-generated method stub
		tv_video.setOnClickListener(this);
		tv_music.setOnClickListener(this);
		vp_main.setOnPageChangeListener(new OnPageChangeListener() {
			//当页面被选中调用该方法
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				changeTitleTextState(position==0);
			}
			
			//当页面滚动的时候调用该方法
			//positionOffset:移动的偏移量的百分比
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// TODO Auto-generated method stub
				scrollIndicator(position,positionOffset);
			}
			
			//当页面的状态发生改变的时候调用该方法
			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	/**
	 * 滚动指示线
	 * @param position
	 * @param positionOffset
	 */
	protected void scrollIndicator(int position, float positionOffset) {
		float translationX = indicatorWidth * position + indicatorWidth * positionOffset;
		ViewHelper.setTranslationX(view_indicator, translationX);
	}

}
